﻿/*******************************************************
 *  单词检测记录管理 相关操作js
 * <p>Title: detectionManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2019年10月30日 下午5:41:57
 * @version 1.0
 * *****************************************************/
var $table, options,//树控件
    indexForm;
var detectionManager = {
    //1.0 初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        detectionManager.bindEvent();
        detectionManager.initTable();
        parent.delLoadBoxs();
    },
    //1.1 初始化Table
    initTable: function () {
        options = {
            url: ctx + "/HeadManager/InitWordDetectionTable",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            singleSelect: false,//多选
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    keyword: $('#keyword').val(),//用户名
                    fileName: $('#fileName').val(),//课件名
                    cpName: $('#cpName').val(),//关卡名
                    word: $('#word').val(),//单词
                    startDate: $('#startDate').val(),//开始日期
                    endDate: $('#endDate').val()//结束日期
                }
                return params;
            },
            columns:[
            {
                checkbox: true
            },{
                field: '',
                title: '序号',
                align: 'center',
                width: 120,
                formatter: function (value, row, index) {                     
                    return index + 1;
                }
            }, {
                field: 'user_name',
                title: '体验用户',
                align: 'center'
            }, {
                field: 'file_name',
                title: '作品',
                align: 'left',
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'cp_name',
                title: '关卡',
                align: 'left',
                formatter: function (value, row, index) {
                    if (value && value.toString().length > 10)
                        back = "<span title='" + value + "'>" + value.toString().substring(0, 10) + "...</span>";
                    else
                        back = value;
                    return back;
                }
            }, {
                field: 'word',
                title: '单词',
                align: 'center'
            }, {
                field: 'detletter',
                title: '考察字母',
                align: 'center'
            }, {
                field: 'choletter',
                title: '选择字母',
                align: 'center'
            }, {
                field: 'status',
                title: '状态',
                align: 'center',
                formatter: function (value, row, index) {
                    if (value === 1)
                        return "<span class='label label-success'>正确</span>";
                    return "<span class='label label-danger'>错误</span>";
                }
            }, {
                field: 'score',
                title: '得分',
                align: 'center'
            }, {
                field: 'create_date',
                title: '创建时间',
                align: 'center'
            }, {
                field: 'duration',
                title: '时长/秒',
                align: 'center'
            }],
            onLoadSuccess: function (data) {
            }
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //1.2 绑定事件
    bindEvent: function () {
        //查询
        $('#btnSearch').on('click', function () {
            $table.bootstrapTable('refresh', options);
        });
        //添加
        //编辑
        $('#wordEdit').on('click', function () {
            var row = detectionManager.selections();
            if (!row)
                return false;
            
        });
        //删除
        $('#wordDel').on('click', function () {
            var row = detectionManager.selections(true);
            if (!row)
                return false;
            var ids = new Array();
            $.each(row, function (index, item) {
                ids.push(item.wd);
            });
            layer.confirm('您确定要删除当前选中单词？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/HeadManager/DelWordDetectionById',
                    dataType: 'json',
                    data: { ids: ids.join(",") },
                    async: false,
                    success: function (data) {
                        if (index)
                            layer.close(index);
                        if (data.Statu === 0) {
                            layer.msg("删除成功...");
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            }, function () { });
        });
    },
    //1.3 获取被选中节点
    selections: function (isAll) {
        var row = $table.bootstrapTable('getSelections');
        if (!row || row.length === 0) {
            layer.msg("数据为空或未选中记录行~", { icon: 5 });
            return false;
        }
        if (isAll)
            return row;
        if (row.length > 1) {
            layer.msg("当前操作不支持多选，请选择一行再操作！", { icon: 5 });
            return false;
        }
        return row[0];
    }
}